<template>
    <div class="payment-page">
      <h2>支付页面</h2>
      <div v-if="loading" class="loading">加载中...</div>
      <div v-else-if="error" class="error">{{ error }}</div>
      <div v-else>
        <img :src="fixedQrCodeUrl" alt="支付二维码" id="qrcode" class="qr-code">
        <button class="payment-button" @click="makePayment">立即支付</button>
      </div>
    </div>
  </template>
  
  <script>
  import instance from '@/utils/request'
  export default {
    name: 'payment',
    data() {
      return {
        loading: false,
        error: '',
        fixedQrCodeUrl: '/pictures/微信支付.jpg' 
      };
    },
    methods: {
      async makePayment() {
        try {
            await instance.put('http://8.219.49.219:8080/onlineShopping/shoppingCar/pay')
            alert('支付成功！');
            this.$router.push({ name: 'home' }); 
        } catch (error) {
            console.log(error)
        }  
      }
    }
  };
  </script>
  
  <style scoped>
  .payment-page {
    max-width: 600px;
    margin: 0 auto;
    padding: 20px;
    border: 1px solid #ccc;
    border-radius: 4px;
    background-color: #f9f9f9;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  }
  
  .loading {
    text-align: center;
    font-size: 16px;
    color: #666;
  }
  
  .error {
    text-align: center;
    font-size: 16px;
    color: #d9534f;
  }
  
  .qr-code {
    margin-top: 20px;
    max-width: 300px; 
    max-height: 300px; 
    display: block; 
    margin-left: auto;
    margin-right: auto;
  }
  
  .payment-button {
    display: block;
    width: 100%;
    padding: 10px 20px;
    margin-top: 20px;
    font-size: 18px;
    color: #fff;
    background-color: #5cb85c;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    transition: background-color 0.3s ease;
  }
  
  .payment-button:hover {
    background-color: #4cae4c;
  }
  </style>